class Solution {
public:
    string addBinary(string a, string b) {
        int i=a.length()-1,j=b.length()-1;
        int carry=0;
        string res="";
        while(i>=0 && j>=0){
            int tot=a[i--]-'0'+b[j--]-'0'+carry;
            carry=tot/2;
            res.insert(0,to_string(tot%2));
        }
        while(i>=0){
            int tot=a[i--]-'0'+carry;
            carry=tot/2;
            res.insert(0,to_string(tot%2));
        }
        while(j>=0){
            int tot=b[j--]-'0'+carry;
            carry=tot/2;
            res.insert(0,to_string(tot%2));
        }
        if(carry==1){res.insert(0,to_string(carry));}
        return res;
    }
};